<?php
class admin_contact_us extends CI_Controller {
	
	protected $_page_name = "admin_contact_us";
	protected $_page_id = 'ul_postcontainer';
	protected $data; //parse template
	protected $submitdata; //submit form --> update database.
	protected $page_title = "Thông Tin Liên Hệ"; // --> page title: index, search
	
	function  __construct()  {
		$this->_initdata();
	}
	
	function _initdata()
	{
		parent::__construct();
		session_start();
		$this->load->model('Adminmodel');
		$this->load->helper('url');
		$this->_check_admin_login();
		
		//init data
		$this->data['base_url']=$this->config->item('base_url');
		$this->data['link_page_name']="admin_contact_us";
		$this->data['page_title']=$this->page_title;
		$this->data['is_addnew']=""; //show link add / list
		$this->data['base_page'] = DEFAULT_PAGE;		
		$this->data['page_id'] = "ul_postcontainer";	
		$this->data['webpage_id'] = "A";
		$this->data['page_name'] = "admin_contact_us";
		$this->data['website_name'] = WEBSITE_NAME;	
                $this->data['admintitle_news'] = ADMINTITLE_NEWS;
                $this->data['admintitle_news_auto'] = ADMINTITLE_NEWS_AUTO;
                $this->data['admintitle_video'] = ADMINTITLE_VIDEO;
                $this->data['admintitle_customs'] = ADMINTITLE_CUSTOMS;
                $this->data['admintitle_adv'] = ADMINTITLE_ADV;
                $this->data['admintitle_faq'] = ADMINTITLE_FAQ;
                $this->data['admintitle_contact'] = ADMINTITLE_CONTACT;
                $this->data['admintitle_common'] = ADMINTITLE_COMMON;
                $this->data['admintitle_stats'] = ADMINTITLE_STATS;
                $this->data['admintitle_beuser'] = ADMINTITLE_BEUSER;
	}			
	
	function _check_admin_login()
	{
		if ($this->Adminmodel->_check_login(SESSION_ADMINUSERNAME) == false)
		{
			$base_url=$this->config->item('base_url');
			redirect($base_url.'admin_login', 'location', 301);
		}		
	}
	
	//default page (tim_kiem)
	function index() {
		//var_dump($this->data);
		//var_dump($this->uri->segment(2));
		$iCurPage=1;
		$iCurPage = $this->uri->segment(2);
		$iRow_Count=0;
		@$limit=PAGESIZE;
		$start=$iCurPage*PAGESIZE-PAGESIZE;
		if(!($start > 0)) { 
		$start = 0;
		}
		$eu = ($start - 0); 
		if(!$limit > 0 ){ 
		$limit = 20; 
		}                             
		list($iRow_Count, $this->data['aContact_us']) = $this->Adminmodel->_get_contact_us_list($eu, $limit);	
		$this->data['paging'] = $this->paging($iCurPage,ceil($iRow_Count/PAGESIZE),$this->config->item('base_url').$this->_page_name,$iRow_Count);
		$this->data['cur_page'] = $iCurPage;
		$this->data['webpage_id'] = 1;	
		$this->data['previous_id'] = 1;			
		$this->data['img_url'] = $this->config->item('img_url');	
		//$this->data['image_type_list'] = $this->phapan->_get_image_type_db();			
		$this->showadmin($this->data);
	}
	
	function check_username()
	{
		echo $this->Adminmodel->_check_user_name($this->uri->segment(3));
	}
	
	function search()
	{
		if (!empty($_POST['isPost']) && ($_POST['isPost']==1)) {
			$webpage_id = $this->uri->segment(3);
			$iCurPage=1;
			$iCurPage = $_POST['h_cur_page'];
			$iRow_Count=0;
			@$limit=PAGESIZE;
			$start=$iCurPage*PAGESIZE-PAGESIZE;
			if(!($start > 0)) { 
			$start = 0;
			}
			$eu = ($start - 0); 
			if(!$limit > 0 ){ 
			$limit = 20; 
			}                             
			list($iRow_Count, $this->data['aContact_us']) = $this->Adminmodel->_search_contact_us_list($_POST['txt_content'], $eu, $limit);	
			$this->data['paging'] = $this->paging_search($iCurPage,ceil($iRow_Count/PAGESIZE),$this->config->item('base_url').$this->_page_name,$iRow_Count);
			$this->data['cur_page'] = 'search';		
			$this->data['txt_content'] = $_POST['txt_content'];
			$this->data['webpage_id'] = $webpage_id;
			$this->data['search'] = 1;	
			$this->data['webpage_id'] = 1;
			$this->data['previous_id'] = 1;			
			$this->data['img_url'] = $this->config->item('img_url');		
			$this->data['page_title']=$this->page_title;				
			$this->showadmin($this->data);
		}
	}
	
	function edit()
	{
		if (!empty($_POST['isPost']) && ($_POST['isPost']==1)) {
			$id =  $_POST['h_id'];
			if ($id != "")
			{
				$this->submitdata['txt_message_reply'] =  $_POST['txt_message_reply'];
				$this->submitdata['h_id'] =  $_POST['h_id'];
				$this->updatecontact_us($this->submitdata);	
			}
			else
			{
				
			}
			//thong bao & redirect page
			//$_SESSION['msg'] = "Bạn đã cập nhật thành công.";
			$this->load->helper('url');
			echo "<script>window.location='".$this->data['base_url'].'admin_contact_us/1'."'</script>";				
		}		
		else
		{
			$this->data["page_name"]="admin_contact_us_edit";
			$this->data['contact_us_detail'] = $this->Adminmodel->_get_contact_us_detail($this->uri->segment(3));	
			$this->data['id'] = $this->uri->segment(3);
			$this->data['webpage_id'] = 1;
			$this->data['previous_id'] = 1;			
			$this->data['img_url'] = $this->config->item('img_url');		
			$this->data['page_name']="admin_contact_us_edit";			
			$this->data['func_type']="edit";	//edit / add
			$this->load->view('common/common_view_admin',$this->data);					
		}
	}
	
	function add()
	{
		$this->data["page_name"]="admin_contact_us_add";
		$this->data['previous_id'] = 1;
		$this->data['func_type']="add";	//edit / add
		$this->load->view('common/common_view_admin',$this->data);
		if (!empty($_POST['isPost']) && ($_POST['isPost']==1)) {
			$this->submitdata['txt_username'] =  $_POST['txt_username'];
			$this->submitdata['txt_password'] =  $_POST['txt_password'];
			$this->submitdata['txt_description'] =  $_POST['txt_description'];
			$this->submitdata['cbo_Status'] =  $_POST['cbo_Status'];
			//insert contact_us
			$this->insertcontact_us($this->submitdata);	
			//thong bao & redirect page
			//$_SESSION['msg'] = "Bạn đã cập nhật thành công.";
			$this->load->helper('url');
			echo "<script>window.location='".$this->data['base_url'].'admin_contact_us/1'."'</script>";				
		}
	}
	
	function insertcontact_us($data){
		$data_insert = array(
						'user_name'  => db_prepare_input($data['txt_username']),
						'password'  => md5($data['txt_password']),						
						'description'  => $data['txt_description'],
						'level'  => 1,
						'status'  => db_prepare_input($data['cbo_Status'])
					 );
		$this->Adminmodel->_insert_contact_us('contact_us', $data_insert);
	}	
	
	function updatecontact_us($data){
		$id =  $data['h_id'];
		$data_update = array(
						'message_reply'  => $data['txt_message_reply'],
						'is_reply'  => 1
					 );
		$this->Adminmodel->_update_contact_us('contact_us','contact_us_id',$id, $data_update);
	}		
	
	function delete()
	{
		$id=($this->uri->segment(3)!="")?$this->uri->segment(3):0;
		if ($id==0)
		{
			echo "fail";
		}
		else
		{
			echo $this->Adminmodel->_delete_contact_us($id);
		}
	}		
	
	//Phân trang dùng cho default page
	function paging($page, $totalpage, $pagelink, $totalrows)
	{
		$paging_str = '';
		$paging_str .= 'Trang: &nbsp;&nbsp;&nbsp;';
		if ($page > 1)
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img21_arrow_left.gif" align="absmiddle" style="cursor:pointer;" onclick="location.href=' ."'".$pagelink.'/'.($page-1)."'". '">';
		else
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img21_arrow_left.gif" align="absmiddle">';
		$paging_str .= '&nbsp;&nbsp;&nbsp;';
		
		// list 10 pagelink
		$startpage = 1;
		if ($page > 5)  $startpage = $page - 4;
		$endpage = $totalpage;
		if ($totalpage - $page > 5)  $endpage = $page + 5;
		for ($i=$startpage; $i<$page; $i++)
			$paging_str .= ' <a href="' .$pagelink.'/'.$i. '" class="link">' .$i. '</a>';
		$paging_str .= ' <a href="' .$pagelink.'/'.$i. '" class="link_selected">' .$page. '</a>';
		for ($i=$page+1; $i<=$endpage; $i++)
			$paging_str .= ' <a href="' .$pagelink.'/'.$i. '" class="link">' .$i. '</a>';
		
		$paging_str .= '&nbsp;&nbsp;&nbsp;';
		if ($page < $totalpage)
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img22_arrow_right.gif" align="absmiddle" style="cursor:pointer;" onclick="location.href=' ."'".$pagelink.'/'.($page+1)."'". '">';
		else
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img22_arrow_right.gif" align="absmiddle">';
		$paging_str .= '&nbsp;&nbsp;&nbsp;</span></td>';
	
		$paging_str .= '&nbsp;&nbsp;&nbsp;<span class="paging_total"><strong>[ </strong>T&#7893;ng c&#7897;ng: <font color="Red">' .$totalrows. '</font> k&#7871;t qu&#7843; <strong> ]</strong></span>';
		return $paging_str;
	}	
	
	//Phân trang dùng cho function "Tìm Kiếm"
	function paging_search($page, $totalpage, $pagelink, $totalrows)
	{
		$paging_str = '';
		$paging_str .= 'Trang: &nbsp;&nbsp;&nbsp;';
		if ($page > 1)
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img21_arrow_left.gif" align="absmiddle" style="cursor:pointer;" onclick="submitSearch(\''.$pagelink. '\','.($page-1).');return true;">';
		else
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img21_arrow_left.gif" align="absmiddle">';
		$paging_str .= '&nbsp;&nbsp;&nbsp;';
		
		// list 10 pagelink
		$startpage = 1;
		if ($page > 5)  $startpage = $page - 4;
		$endpage = $totalpage;
		if ($totalpage - $page > 5)  $endpage = $page + 5;
		for ($i=$startpage; $i<$page; $i++)
			$paging_str .= ' <a onclick="submitSearch(\''.$pagelink. '\','.$i.');return true;" class="link" href="#">'.$i.'</a>';
			//$paging_str .= ' <a onclick="submitSearch(\'"'.$pagelink. '></a>';;
		//$paging_str .= ' <a href="' .$pagelink.'/'.$i. '" class="link_selected">' .$page. '</a>';
		$paging_str .= ' <a onclick="submitSearch(\''.$pagelink. '\','.$i.');return true;" class="link_selected" href="#">'.$i.'</a>';
		for ($i=$page+1; $i<=$endpage; $i++)
			$paging_str .= ' <a onclick="submitSearch(\''.$pagelink. '\','.$i.');return true;" class="link" href="#">'.$i.'</a>';
			//$paging_str .= ' <a href="' .$pagelink.'/'.$i. '" class="link">' .$i. '</a>';
		
		$paging_str .= '&nbsp;&nbsp;&nbsp;';
		if ($page < $totalpage)
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img22_arrow_right.gif" align="absmiddle" style="cursor:pointer;" onclick="submitSearch(\''.$pagelink. '\','.($page+1).');return true;">';
		else
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img22_arrow_right.gif" align="absmiddle">';
		$paging_str .= '&nbsp;&nbsp;&nbsp;</span></td>';
	
		$paging_str .= '&nbsp;&nbsp;&nbsp;<span class="paging_total"><strong>[ </strong>T&#7893;ng c&#7897;ng: <font color="Red">' .$totalrows. '</font> k&#7871;t qu&#7843; <strong> ]</strong></span>';
		return $paging_str;
	}		
	
}
?>